Load data.
gun_violence_data = read_csv("./data/gun_violence_data_2013_2018.csv")
## Parsed with column specification:
## cols(
## .default = col_character(),
## incident_id = col_integer(),
## date = col_date(format = ""),
## n_killed = col_integer(),
## n_injured = col_integer(),
## congressional_district = col_integer(),
## latitude = col_double(),
## longitude = col_double(),
## n_guns_involved = col_integer(),
## state_house_district = col_integer(),
## state_senate_district = col_integer()
## )
## See spec(...) for full column specifications.
Tidy data.
gun_v_tidy =
gun_violence_data %>%
select(date:city_or_county, n_killed, n_injured, latitude, longitude) %>%
separate(date, into = c("year", "month", "day"), sep = "-") %>%
mutate(n_affected = n_killed + n_injured) %>%
filter(n_affected > 0) %>%
mutate(mass_shooting = ifelse(n_affected >= 4, "Yes", "No"))
Centered latitude and longitude points for each state.
url = "https://inkplant.com/code/state-latitudes-longitudes"
coord_webpage = read_html(url)
cent_lat_and_long =
coord_webpage %>%
html_nodes(css = "table") %>%
.[[1]] %>%
html_table(header = TRUE) %>%
as_tibble() %>%
janitor::clean_names() %>%
filter(state != "Alaska", state != "Hawaii")
Bubble map of mass shootings by state.
n_shooting_inc =
gun_v_tidy %>%
filter(mass_shooting == "Yes") %>%
group_by(state) %>%
summarize(n_incidents = n())
usa_shooting_inc =
inner_join(n_shooting_inc, cent_lat_and_long, by = "state")
usa = map_data("state")
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
ggplot() +
geom_path(data = usa, aes(x = long, y = lat, group = group)) +
geom_point(data = usa_shooting_inc, aes(x = longitude, y = latitude, size = n_incidents), color = "darkseagreen3") +
scale_size(range = c(0, 12))
Plotly interactive map of shootings by state.
gun_v_tidy %>%
filter(state != "Alaska", state != "Hawaii", longitude < 0) %>%
mutate(n_affected_cat = ifelse(n_affected %in% 4:9, "2: 4-9",
ifelse(n_affected %in% 10:19, "3: 10-19",
ifelse(n_affected %in% 20:34, "4: 20-34",
ifelse(n_affected %in% 35:49, "5: 35-49",
ifelse(n_affected > 50, "6: 50+", "1: 1-3")))))) %>%
mutate(city_state = str_c(city_or_county, state, sep = ", ")) %>%
mutate(text_label = str_c(city_state, '\n', year,'\nKilled: ', n_killed, '\nInjured: ', n_injured)) %>%
plot_ly(x = ~longitude, y = ~latitude, type = "scatter", mode = "markers",
alpha = 0.5,
color = ~n_affected_cat,
colors = "Accent",
text = ~text_label)